<template>
  <div class="category">
    <h1>{{ id ? "编辑" : "新建" }}英雄</h1>
    <el-form label-width="120px" v-model="form" @submit.native.prevent="save">
      <el-tabs type="border-card">
        <el-tab-pane label="基本信息">
          <el-form-item label="英雄名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="称号">
            <el-input v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="角色">
            <el-upload
              class="avatar-uploader"
              :action="$http.defaults.baseURL + '/upload'"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="form.icon" :src="form.icon" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="form.types" multiple>
              <el-option
                v-for="item in categories"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度">
            <el-rate
              style="margin-top: 0.6rem"
              show-score
              :max="9"
              v-model="form.scores.difficult"
            >
            </el-rate>
          </el-form-item>
          <el-form-item label="技能">
            <el-rate
              style="margin-top: 0.6rem"
              show-score
              :max="9"
              v-model="form.scores.skill"
            >
            </el-rate>
          </el-form-item>
          <el-form-item label="攻击">
            <el-rate
              style="margin-top: 0.6rem"
              show-score
              :max="9"
              v-model="form.scores.attack"
            >
            </el-rate>
          </el-form-item>
          <el-form-item label="生存">
            <el-rate
              style="margin-top: 0.6rem"
              show-score
              :max="9"
              v-model="form.scores.survive"
            >
            </el-rate>
          </el-form-item>
          <el-form-item label="顺风出装">
            <el-select v-model="form.items1" multiple>
              <el-option
                v-for="item in items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装">
            <el-select v-model="form.items2" multiple>
              <el-option
                v-for="item in items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧">
            <el-input type="textarea" v-model="form.usageTips"></el-input>
          </el-form-item>
          <el-form-item label="对战技巧">
            <el-input type="textarea" v-model="form.battleTips"></el-input>
          </el-form-item>
          <el-form-item label="团战技巧">
            <el-input type="textarea" v-model="form.teamTips"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="技能">
          <el-button @click="form.skills.push({})">
            <i class="el-icon-plus"></i> 添加技能</el-button
          >
          <el-row type="flex" style="flex-wrap: wrap">
            <el-col :md="12" v-for="(item, index) in form.skills" :key="index">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL + '/upload'"
                  :show-file-list="false"
                  :on-success="(res) => $set(item, 'icon', res.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input type="textarea" v-model="item.tips"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="danger" @click="form.skills.splice(index, 1)"
                  >删除</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item style="margin-top: 1rem">
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
/* eslint-disable */
export default {
  name: "HeroAddEdit",
  props: {
    id: {},
  },
  data() {
    return {
      form: {
        name: "", // 名称
        icon: "", // 英雄图标
        scores: {}, // 难度
        skills: [], // 技能
      },
      categories: [], // 类型选择
      items: [], // 顺风出装
    };
  },
  created() {
    this.id && this.edit(); // 判断是否有id,有则执行后面的方法
    this.getParentOptions();
    this.getCategories(); // 调用获得分类接口
    this.getItems(); // 获得装备接口
  },
  mounted() {},
  methods: {
    // 获得分类列表
    save() {
      let res;
      if (this.id) {
        // 如果存在id，则调用编辑的接口
        res = this.$http.put(`rest/hero/${this.id}`, this.form); // 调用这个接口
      } else {
        res = this.$http.post("rest/hero", this.form); // 调用这个接口
      }
      this.$router.push("/hero/list"); // 路由跳转
      this.$message({
        type: "success",
        message: "保存成功！！！",
      });
    },
    // 获得详情
    async edit() {
      const res = await this.$http.get(`rest/hero/${this.id}`); // 调用接口
      this.form = Object.assign({}, this.form, res.data); // 将查询到的数据返回到页面
    },
    // 获得详情
    async getParentOptions() {
      const res = await this.$http.get(`rest/hero`); // 调用接口
      this.parentOptions = res.data; // 浅拷贝数据
    },
    // 获得类型
    async getCategories() {
      const res = await this.$http.get(`rest/categories`); // 调用接口
      this.categories = res.data; // 将查询到的数据返回到页面
    },
    // 获得顺风出装
    async getItems() {
      const res = await this.$http.get(`rest/equip`); // 调用接口
      this.items = res.data; // 将查询到的数据返回到页面
    },
    // 处理成功后的图片
    handleAvatarSuccess(res, file) {
      this.form.icon = res.url; /*  */
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 删除接口
    async handleDelSkills(row) {
      const res = await this.$http.delete(`rest/hero/${row._id}`);
      this.$message({
        type: "success",
        message: "删除成功!",
      });
    },
  },
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 5rem;
  height: 5rem;
  line-height: 5rem;
  text-align: center;
}
.avatar {
  width: 5rem;
  height: 5rem;
  display: block;
}
</style>
